/// <reference path="../Resources/js/jquery/jquery.debug.js" />
/// <reference name="MicrosoftAjax.debug.js" />

Type.registerNamespace("BM");

function MForm(element) {
    MForm.initializeBase(this, [element]);
};

MForm.prototype = {
    add_itemCreated: function(handler) {
        /// <summary>
        /// Add an event handler for the itemCreated event
        /// </summary>
        /// <param name="handler" type="Function" mayBeNull="false">
        /// Event handler
        /// </param>
        /// <returns />
        this.get_events().addHandler('itemCreated', handler);
    },

    remove_itemCreated: function(handler) {
        /// <summary>
        /// Remove an event handler from the itemCreated event
        /// </summary>
        /// <param name="handler" type="Function" mayBeNull="false">
        /// Event handler
        /// </param>
        /// <returns />
        this.get_events().removeHandler('itemCreated', handler);
    },

    raise_itemCreated: function(eventArgs) {
        /// <summary>
        /// Raise the itemCreated event
        /// </summary>
        /// <param name="eventArgs" type="Sys.EventArgs" mayBeNull="false">
        /// Event arguments for the itemCreated event
        /// </param>
        /// <returns />

        var handler = this.get_events().getHandler('itemCreated');
        if (handler) {
            handler(this, eventArgs);
        }
    },

    add_itemRemoved: function(handler) {
        /// <summary>
        /// Add an event handler for the itemRemoved event
        /// </summary>
        /// <param name="handler" type="Function" mayBeNull="false">
        /// Event handler
        /// </param>
        /// <returns />
        this.get_events().addHandler('itemRemoved', handler);
    },

    remove_itemRemoved: function(handler) {
        /// <summary>
        /// Remove an event handler from the itemRemoved event
        /// </summary>
        /// <param name="handler" type="Function" mayBeNull="false">
        /// Event handler
        /// </param>
        /// <returns />
        this.get_events().removeHandler('itemRemoved', handler);
    },

    raise_itemRemoved: function(eventArgs) {
        /// <summary>
        /// Raise the itemRemoved event
        /// </summary>
        /// <param name="eventArgs" type="Sys.EventArgs" mayBeNull="false">
        /// Event arguments for the itemRemoved event
        /// </param>
        /// <returns />

        var handler = this.get_events().getHandler('itemRemoved');
        if (handler) {
            handler(this, eventArgs);
        }
    },

    add_loadComplete: function(handler) {
        /// <summary>
        /// Add an event handler for the loadComplete event
        /// </summary>
        /// <param name="handler" type="Function" mayBeNull="false">
        /// Event handler
        /// </param>
        /// <returns />
        this.get_events().addHandler('loadComplete', handler);
    },

    remove_loadComplete: function(handler) {
        /// <summary>
        /// Remove an event handler from the loadComplete event
        /// </summary>
        /// <param name="handler" type="Function" mayBeNull="false">
        /// Event handler
        /// </param>
        /// <returns />
        this.get_events().removeHandler('loadComplete', handler);
    },

    raise_loadComplete: function(eventArgs) {
        /// <summary>
        /// Raise the loadComplete event
        /// </summary>
        /// <param name="eventArgs" type="Sys.EventArgs" mayBeNull="false">
        /// Event arguments for the loadComplete event
        /// </param>
        /// <returns />

        var handler = this.get_events().getHandler('loadComplete');
        if (handler) {
            handler(this, eventArgs);
        }
    },

    set_culturalContext: function(value) {
        this.culturalContext = new BM.CulturalContext(value);
    },

    get_culturalContext: function() {
        return this.culturalContext;
    },    

    get_shouldValidateInViewMode: function() {
        return this.shouldValidateInViewMode;
    },

    set_shouldValidateInViewMode: function(value) {
        this.shouldValidateInViewMode = value;
    },    

    get_mode: function() {
        return this.mode;
    },

    set_mode: function(value) {
        this.mode = value;
    },

    get_idSeparator: function() {
        return this.idSeparator;
    },

    set_idSeparator: function(value) {
        this.idSeparator = value;
    },

    dispose: function() {
        MForm.callBaseMethod(this, 'dispose');
        this.culturalContext = null;
    },

    initialize: function() {
        MForm.callBaseMethod(this, 'initialize');
        Sys.Application.add_load(Function.createDelegate(this, this.load));
    },

    getRootItem: function() {
        return MItem.getById(this._element.id + '_1');
    },

    load: function() {
        this.registerItems();
        this.getRootItem().load();
        this.raise_loadComplete();
    },

    registerItems: function() {
        var elems = $(this._element).find("." + MItem.Element);
        for (var i = 0; i < elems.length; i++) {
            var elem = elems[i];
            if (elem.id.endsWith('_0')) {
                elem.parentNode.removeChild(elem);
                elem.style.display = '';
                MBasis.getById(elem.id).setPrototypeNode(elem);
            } else if (elem.id.indexOf('_0_') != -1) {
                MBasis.getById(elem.id).setPrototypeNode(elem);
                continue;
            } else {
                var basis = MBasis.getById(elem.id);
                basis.setPrototypeNode(elem);
                basis.createInstance(elem);
            }
        }
    }
}

MForm.registerClass("MForm", Sys.UI.Control);

MForm.focus = function(field) {
    try {
        if (field && field.focus) {
            field.focus();
        }
        return true;
    } catch (e) {
    }
    return false;
}
